package com.informatics.polymer.server.browserepository;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import org.apache.commons.io.FileUtils;
import org.openscience.cdk.exception.CDKException;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import com.informatics.polymer.server.DescriptorCalculation.MolecularDescriptorCalculation;
import com.informatics.polymer.server.Polymerisation.Homopolymerisation;
import com.informatics.polymer.server.Polymerisation.PolymerisationUtils;
import com.informatics.polymer.server.imagegeneration.ImageGenerator;

/**
 * Restlet to add repeat units to a derby database.
 * 
 * @author ed
 * @version 1.0
 */
public class AddingRepeatUnit extends Resource {
	/**
	 * Repeat unit to add to the database.
	 */
	private String repeatUnit;

	/**
	 * Sets repeat unit.
	 * 
	 * @param repeatUnitValue
	 *            - smiles string representation of the repeat unit.
	 */
	private void setRepeatUnit(final String repeatUnitValue) {
		this.repeatUnit = repeatUnitValue;
	}

	/**
	 * Gets repeat unit.
	 * 
	 * @return String representation of the repeat unit.
	 */
	private String getRepeatUnit() {
		return repeatUnit;
	}

	/**
	 * Initiate the restlet.
	 */
	public void init(Context context, Request request, Response response) {
		super.init(context, request, response);
		// getVariants().add(Constants.XML_VARIANT);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
		this.setModifiable(true);
	}

	/**
	 * Method allows forms to be posted to restlet.
	 */
	@Override
	public boolean allowPost() {
		return true;
	}

	/**
	 * Constructs AddingRepeatUnit resource.
	 * 
	 * @param context
	 * @param request
	 * @param response
	 */
	public AddingRepeatUnit(Context context, Request request, Response response) {
		super(context, request, response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}

	/**
	 * Method deals with the form posted to the restlet.
	 */
	@Override
	public void post(Representation entity) {
		Form form = new Form(entity);
		setRepeatUnit(form.getValues("repeatUnit"));
		MolecularDescriptorCalculation molecularDescriptorCalculation = new MolecularDescriptorCalculation();
		try {
			HashMap<String, Double> descriptors = molecularDescriptorCalculation
					.calculateAllDescriptors(getRepeatUnit());
		} catch (CDKException e) {
			e.printStackTrace();
		}
		//Now generate the picture and write out to a file
		ImageGenerator imgG = new ImageGenerator(getRepeatUnit());
		try {
			imgG.writeOutImage();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		StringRepresentation str = new StringRepresentation("Test");
		/*
		 * //Write out to a file String f = "/Homopolymer"+counterValue+".cml";
		 * String curDir = new String(System.getProperty("user.dir")); File
		 * homopolymerXml = new File(curDir+f); //FileRepresentation fr = new
		 * FileRepresentation(finalResult, null); BufferedWriter out; //Use
		 * apache io try { FileUtils.writeStringToFile(homopolymerXml,
		 * finalResult); } catch (IOException e1) { e1.printStackTrace(); } try
		 * { out = new BufferedWriter(new
		 * FileWriter(curDir+"/Homopolymer.cml")); out.write(finalResult); //
		 * w.write(out); out.close(); } catch (IOException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); }
		 */
		getResponse().setEntity(str);
	}

	// Respond to GET
	public Representation represent(Variant variant) throws ResourceException {
		Representation resource = null;
		if (!MediaType.TEXT_PLAIN.includes(variant.getMediaType())) {
			throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
		} else {
			StringBuffer sb = new StringBuffer();
			try {
			} catch (Exception e) {
				e.printStackTrace();
			}
			resource = new StringRepresentation(sb.toString(),
					MediaType.TEXT_PLAIN);
			return resource;
		}
	}

}
